//表单验证
$(function(){
	$("#salePrice").TouchSpin({
        min: 0,
        max: 10000000,
        step: 0.1,
        decimals: 2,
        boostat: 5,
        maxboostedstep: 10,
        buttondown_class: 'btn btn-white',
        buttonup_class: 'btn btn-white'
    });
	$("#marketPrice").TouchSpin({
        min: 0,
        max: 10000000,
        step: 0.1,
        decimals: 2,
        boostat: 5,
        maxboostedstep: 10,
        buttondown_class: 'btn btn-white',
        buttonup_class: 'btn btn-white'
    });
	$("#costPrice").TouchSpin({
        min: 0,
        max: 10000000,
        step: 0.1,
        decimals: 2,
        boostat: 5,
        maxboostedstep: 10,
        buttondown_class: 'btn btn-white',
        buttonup_class: 'btn btn-white'
    });
	$("#sortNo").TouchSpin({
        min: 1,
        buttondown_class: 'btn btn-white',
        buttonup_class: 'btn btn-white'
    });
	function updateProduct(type){
		//判断冻结标记"frezz"
		var buttonFlag = $('#buttonFlag').val();
		var nowFlag = new Date().valueOf();
		if(buttonFlag != null && buttonFlag != ""){
			if(nowFlag - buttonFlag < 2000){
				return;
			}
		}
		//设置冻结
		$('#buttonFlag').val(nowFlag);
		//获取form的值
		var id = $('#id').val();
		var name = $('#name').val();
		var salePrice = $('#salePrice').val();
		var marketPrice = $('#marketPrice').val();
		var costPrice = $('#costPrice').val();
		var sortNo = $('#sortNo').val();
		var description = $('#description').val();
		var status = $('#status').val();
		var picMainImage = $('#picMainImage').attr("src");
		var pic1Image = $('#pic1Image').attr("src");
		var pic2Image = $('#pic2Image').attr("src");
		var pic3Image = $('#pic3Image').attr("src");
		
		var spec = $('#spec').val();
		var cate = $('#cate').val();
		var brand = $('#brand').val();
		var productionPlace = $('#productionPlace').val();
		//是不是草稿，下单也必须有商品name
		if(name == null || name == ""){
			layer.msg("请输入商品名称!",{icon:2,time:2000});
			return;
		}
		//如果不是草稿，需以下校验
		if(type != 0){
			if(salePrice == null || salePrice == ""){
				layer.msg("请输入商品售价!",{icon:2,time:2000});
				return;
			}
			
			if(picMainImage == null || picMainImage == ""){
				layer.msg("请上传商品图片,方便向客户展示!",{icon:2,time:2000});
				return;
			}
		}
		if(name != null && name.length > 32){
			layer.msg("商品名称不能超过32位!",{icon:2,time:2000});
			return;
		}
		if(picMainImage != null && picMainImage.length > 512){
			layer.msg("商品图片url不能超过512位!",{icon:2,time:2000});
			return;
		}
		if(description != null && description.length > 512){
			layer.msg("店铺描述不能超过512位!",{icon:2,time:2000});
			return;
		}
		if(spec != null && spec.length > 32){
			layer.msg("规格不能超过32位!",{icon:2,time:2000});
			return;
		}
		if(cate != null && cate.length > 32){
			layer.msg("类别不能超过32位!",{icon:2,time:2000});
			return;
		}
		if(brand != null && brand.length > 32){
			layer.msg("品牌不能超过32位!",{icon:2,time:2000});
			return;
		}
		if(productionPlace != null && productionPlace.length > 100){
			layer.msg("产地不能超过100位!",{icon:2,time:2000});
			return;
		}
		var product = {};
		product["id"] = id;
		product["name"] = name;
		product["salePrice"] = salePrice;
		product["marketPrice"] = marketPrice;
		product["costPrice"] = costPrice;
		product["sortNo"] = sortNo;
		product["description"] = description;
		//0是草稿状态
		if(type == 0){
			//-1：删除，0，草稿，1，正常
			product["dr"] = 0;
		}
		product["status"] = status;
		
		product["picMain"] = picMainImage;
		product["pic1"] = pic1Image;
		product["pic2"] = pic2Image;
		product["pic3"] = pic3Image;
		
		product["spec"] = spec;
		product["cate"] = cate;
		product["brand"] = brand;
		product["productionPlace"] = productionPlace;
		
		$.ajax({
			url:baselocation+'/product',
			contentType:'application/json',
			type:'post',
			dataType:'json',
			data: JSON.stringify(product),
			success:function(result){
				if(result.success==false){
					layer.msg(result.message,{icon:2,time:2000});
				}else{
					window.location.href=baselocation+'/product/'+result.data+'/details';
				}
			},
			error:function(result){
				alert("系统异常,请联系管理员！");
			}
		});
	}
	
	$('#saveBnt').click(function () {
		updateProduct(1);
	})
	
	$('#caogaoBnt').click(function () {
		updateProduct(0);
	})
});